<?php

/**
 * 项目文件管理器
 * @since 1.0 2014-7-31 by sutroon
 */
// =============================通用函数=====================================
/**
 * 获取表单参数
 * @param string $name
 * @param mixed $defv
 * @return mixed
 * @since 1.0 2014-7-25 by sutroon
 */
function I($name, $defv = '') {
    if (isset($_GET[$name])) {
        return $_GET[$name];
    }
    if (isset($_POST[$name])) {
        return $_POST[$name];
    }
    return $defv;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>SoProjectManager</title>
        <style type="text/css">
            body {font-size:12px; color:#333;}
            a { text-decoration:none; color:#0078E7}
            .list div { display:inline-block; width:64px; height:78px; margin: 10px 10px 0px 0px; overflow: hidden; text-align: center;}
            .list img { width:64px; height:64px; display: block;}
        </style>
    </head>
    <body>
        <?php
        // 全局菜单
        include 'Inc/incMenu.php';
        
        $root = $_SERVER['DOCUMENT_ROOT'];
        $path = I('path');
        $location = '<a href="?path=/">/</a>';
        if ($path) {
            if (substr($path, 0, strlen($root)) !== $root) {
                die('路径有误! <a href="?path=' . $root . '">[返回]</a>');
            }
            $dirs = substr($path, strlen($root) + 1);
            $arrdir = explode('\\', $dirs);
            $pathpart = $root;
            foreach ($arrdir as $dir) {
                $pathpart.='\\' . $dir;
                $location.=' &raquo; <a href="?path=' . $pathpart . '">' . $dir . '</a>';
            }
        }
        echo "<div class='location'>$location</div>";
        $path = $path ? $path : $root;
        echo '<div class="list">';
        show_dirs($path);
        echo '</div>';

        $action = I('action');
        // 2014-8-4 编辑文件
        if ($action == 'edit') {
            $file = I('file');
            echo '编辑:' . $file;
            echo '<form method="post" action="?action=savefile">';
            echo '<textarea name="content" rows="30" cols="90">' . htmlentities(file_get_contents($file),0,'UTF-8') . '</textarea><br />';
            echo '<input type="submit" value="保存" /><input type="reset" value="重置" />';
            echo '<input type="hidden" name="action" value="savefile" /><input type="hidden" name="path" value="' . I('path') . '" /><input type="hidden" name="file" value="' . I('file') . '" />';
            echo '</form>';
        } else if ($action == 'savefile') { // 保存文件
            $file = I('file');
            file_put_contents($file, I('content'));
            $path = str_replace('\\', '\\\\',I('path'));
            die('<script>location.href="?path=' . $path . '";</script>');
        }

        function show_dirs($path) {
            $arr = array();
            list_dir($path, $arr);
            foreach ($arr as $row) {
                if ($row['ext'] == 'folder') {
                    echo '<div class="item folder" title="' . $row['name'] . '"><a href="?path=' . $row['path'] . '" title="' . $row['name'] . '"><img src="Skin/folder64.png" alt="' . $row['name'] . '" />' . subtext($row['name']) . '</a></div>';
                }
            }
            $ext;
            foreach ($arr as $row) {
                if ($row['ext'] == 'folder') {
                    continue;
                }
                $ext = ',' . $row['ext'] . ',';
                if (strpos(',png,jpg,jpeg,gif,bmp,', $ext) !== false) {
                    echo '<div class="item img"><a href="' . $row['path'] . '" title="' . $row['name'] . '"><img src="' . $row['path'] . '" title="' . $row['name'] . '" />' . subtext($row['name']) . '</a></div>';
                } else if (strpos(',conf,txt,php,asp,aspx,cs,vb,as,js,css,xml,htm,html,xhtml,', $ext) !== false) {
                    echo '<div class="item file"><a href="?path=' . substr($row['path'], 0, strrpos($row['path'], '\\')) . '&file=' . $row['path'] . '&action=edit" title="' . $row['name'] . '"><img src="Skin/file64.png" title="' . $row['name'] . '" />' . subtext($row['name']) . '</a></div>';
                } else {
                    echo '<div class="item file"><a href="' . $row['path'] . '" target="_blank" title="' . $row['name'] . '"><img src="Skin/file64.png" title="' . $row['name'] . '" />' . subtext($row['name']) . '</a></div>';
                }
            }
        }

        function list_dir($dir_path, &$arr) {
            if ($handle = opendir($dir_path)) {
                $path;
                $arritem;
                while (false !== ($file = readdir($handle))) {
                    if ($file == "." || $file == "..") {
                        continue;
                    }
                    $path = $dir_path . '\\' . $file;
                    if (is_dir($path)) {
                        $arritem = array('name' => $file, 'path' => $path, 'ext' => 'folder');
                        //list_dir($path, $arritem['sub']);
                        $arr[] = $arritem;
                        continue;
                    }
                    $arr[] = array('name' => $file, 'path' => $path, 'ext' => sofunc_get_file_ext($path));
                }
                closedir($handle);
            }
        }

        function subtext($str, $len = 9) {
            return (strlen($str) <= $len) ? $str : substr($str, 0, $len);
        }

        /**
         * 获取文件后缀名(小写,不带点号)
         * @param string $path
         * @return string
         * @since 1.0 2014-6-24 by sutroon
         */
        function sofunc_get_file_ext($path) {
            return strtolower(substr($path, strrpos($path, '.') + 1));
        }
        ?>
    </body>
</html>
